import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

/**
 * Created by ospip on 2017/11/14.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class BallCanvas extends JPanel implements ActionListener {
    private LinkedList<Ball> list;
    private Timer timer;
    private int a = 0;
    public BallCanvas() {
        this.setSize(800, 600);
        list = new LinkedList<>();
        timer = new Timer(10, this);
        timer.start();
    }

    public void add() throws InterruptedException {
        Ball b = new Ball(this.getWidth(), this.getHeight());
        System.out.println("add");
        list.add(b);
        new BallThread(b).start();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        for (Ball b : list) {
            //b.update();
            if (b.x - b.radius <= b.borderx) g.fillOval(b.x, b.y - b.radius, b.radius, b.radius);
            else {
                list.remove(b);
                System.out.println("remove");
                break;
            }
        }
        try {
            a ++;
            if (a == 10) {
                //add();
                a = 0;
            }
        }
        catch(Exception e) {}
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
}
